package server.configuration;

import lombok.Data;
import server.HttpServlet;

import java.util.Map;

/**
 * Mapper组件结构
 *
 * @author leonzhangxf
 */
@Data
public class Mapper {

    private Map<String, MapperHost> hostMap;

    public HttpServlet getServlet(String url) {
        String[] urlSegment = url.split("/");

        MapperHost mapperHost = hostMap.get("localhost");
        if (null == mapperHost) {
            return null;
        }

        MapperContext mapperContext = mapperHost.getContextMap().get(urlSegment[1]);
        if (null == mapperContext) {
            return null;
        }

        StringBuilder servletUrl = new StringBuilder();
        for (int i = 2; i < urlSegment.length; i++) {
            servletUrl.append("/").append(urlSegment[i]);
        }
        return mapperContext.getServletMap().get(servletUrl.toString());
    }
}
